package com.bosssoft.springboot.demo2.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @description webSocket配置文件，为Spring配置STOMP协议的处理
 * @author ChenTong
 * @date 2020/6/10
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * @description 实现接口方法配置消息broker
     * 它首先调用enableSimpleBroker（）启用一个基于内存的简单消息代理，
     * 将问候消息在返回到以/topic为前缀的客户端。
     * 它还为与@MessageMapping注释的方法绑定的消息指定/app前缀。此前缀将用于定义所有消息映射。
     * 例如，/ app / hello是GreetingController.greeting（）方法映射到处理的端点。
     * @param registry
     * @return void
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        registry.enableSimpleBroker("/topic");
        //定义前缀
        registry.setApplicationDestinationPrefixes("/app");
    }

    /**
     * @description registerStompEndpoints（）方法注册/ gs-guide-websocket端点
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").withSockJS();
    }
}
